﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Linq;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;
using System.Workflow.ComponentModel;


namespace Sogeti.Workflow.Activities
{
	public partial class UpdateListItem: Activity
    {
        public static DependencyProperty __ContextProperty = System.Workflow.ComponentModel.DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(UpdateListItem));

        [Description("Context")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public WorkflowContext __Context
        {
            get
            {
                return ((WorkflowContext)(base.GetValue(UpdateListItem.__ContextProperty)));
            }
            set
            {
                base.SetValue(UpdateListItem.__ContextProperty, value);
            }
        }

        public static DependencyProperty ListIdProperty = System.Workflow.ComponentModel.DependencyProperty.Register("ListId", typeof(string), typeof(UpdateListItem));

        [Description("ListId")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ListId
        {
            get
            {
                return ((string)(base.GetValue(UpdateListItem.ListIdProperty)));
            }
            set
            {
                base.SetValue(UpdateListItem.ListIdProperty, value);
            }
        }

        
        public static DependencyProperty ListItemProperty = System.Workflow.ComponentModel.DependencyProperty.Register("ListItem", typeof(int), typeof(UpdateListItem));

        [Description("ListItem")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int ListItem
        {
            get
            {
                return ((int)(base.GetValue(UpdateListItem.ListItemProperty)));
            }
            set
            {
                base.SetValue(UpdateListItem.ListItemProperty, value);
            }
        }

        public static DependencyProperty ItemPropertiesProperty = System.Workflow.ComponentModel.DependencyProperty.Register("ItemProperties", typeof(Hashtable), typeof(UpdateListItem));

        [Description("Item Properties")]
        [ValidationOption(ValidationOption.Required)]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Hashtable ItemProperties
        {
            get
            {
                return ((Hashtable)(base.GetValue(UpdateListItem.ItemPropertiesProperty)));
            }
            set
            {
                base.SetValue(UpdateListItem.ItemPropertiesProperty, value);
            }
        }
        
		public UpdateListItem()
		{
			InitializeComponent();
		}

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                //elevate privilages, because SHAREPOINT\System account has full access privilages to the entire farm
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite sourceSite = new SPSite(this.__Context.Web.Site.ID))
                    {
                        using (SPWeb sourceWeb = sourceSite.AllWebs[this.__Context.Web.ID])
                        {
                            System.Guid listGUID = new Guid(this.ListId);
                            SPList theList = sourceWeb.Lists[listGUID];
                            SPListItem theItem = theList.GetItemById(this.ListItem);

                            ICollection keys = this.ItemProperties.Keys;
                            foreach (string key in keys)
                            {
                                theItem[key] = ItemProperties[key].ToString();
                            }

                            theItem.Update();
                        }
                    }

                });
            }
            catch (Exception e)
            {

            }

            return ActivityExecutionStatus.Closed;
        }
	}
}
